package net.melon.product.inst.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.esj.basic.core.view.tools.ActionContext;
import net.esj.basic.dao.jdbc.JdbcDao;
import net.esj.basic.dao.querybuilder.QueryBuilder;
import net.esj.basic.exception.MelonRuntimeException;
import net.esj.basic.service.impl.DefaultServiceImpl;
import net.melon.product.inst.service.SpecificationsValueService;
import net.melon.product.pojo.ProductClassify;
import net.melon.product.pojo.ProductSpecificationsValue;
import net.melon.uploader.Uploader;
import net.melon.uploader.pojo.FileUpload;
import net.melon.uploader.service.impl.DateSplitPathRule;
import net.melon.uploader.service.impl.RandomFileNameRule;

@Service("specificationsValueService")
public class SpecificationsValueServiceImpl extends DefaultServiceImpl implements
		SpecificationsValueService {

	@Override
	public ProductSpecificationsValue saveProductSpecificationsValue(ProductSpecificationsValue entity) {
		try {
			Uploader uploader = new Uploader(new DateSplitPathRule(), new RandomFileNameRule(), ProductSpecificationsValue.ICON_DIR);
			uploader.setAllowFiles(Uploader.ALLOW_FILE_IMAGES);
			List<FileUpload> list = uploader.upload((HttpServletRequest)ActionContext.getRequest());
			if(!list.isEmpty()){
				FileUpload up = list.get(0);
				entity.setImage(up.getUrl());
			}
			entity.setValue(entity.getValue());
		} catch (Exception e) {
			throw new MelonRuntimeException(e);
		}
		save(entity);
		return entity;
	}

	@Override
	public ProductSpecificationsValue updateProductSpecificationsValue(ProductSpecificationsValue entity) {
		try {
			Uploader uploader = new Uploader(new DateSplitPathRule(), new RandomFileNameRule(), ProductSpecificationsValue.ICON_DIR);
			uploader.setAllowFiles(Uploader.ALLOW_FILE_IMAGES);
			List<FileUpload> list = uploader.upload((HttpServletRequest)ActionContext.getRequest());
			if(!list.isEmpty()){
				FileUpload up = list.get(0);
				entity.setImage(up.getUrl());
			}
			entity.setValue(entity.getValue());
		} catch (Exception e) {
			throw new MelonRuntimeException(e);
		}
		update(entity);
		return entity;
	}

	@Override
	public ProductSpecificationsValue getProductSpecificationsValue(Long id) {
		return get(id, ProductSpecificationsValue.class);
	}

	@Override
	public void deleteProductSpecificationsValue(Long id) {
		deleteProductSpecificationsValue(id, false);
	}

	@Override
	public Map<Long,ProductSpecificationsValue> getList() {
		QueryBuilder<ProductSpecificationsValue> qb = createQueryBuilder(ProductSpecificationsValue.class);
		qb.eq("state", 1).fetch("specifications");
		List<ProductSpecificationsValue> list = queryListByQueryBuilder(qb);
		Map<Long,ProductSpecificationsValue> map = new HashMap<Long, ProductSpecificationsValue>();
		for(ProductSpecificationsValue value : list){
			map.put(value.getId(), value);
		}
		return map;
	}

	@Transactional
	@Override
	public void deleteProductSpecificationsValue(Long id, boolean withProduct) {
		ProductSpecificationsValue value = getProductSpecificationsValue(id);
		delete(value);
		if(withProduct){
			getDao(JdbcDao.class).getJdbcTemplate().update("DELETE FROM `ml_product_specifications_link` WHERE SPECIFICATIONS_VALUE = ? ", new Object[]{id});
		}
	}

}
